import os
import time
from datetime import datetime


def get_video_creation_time(video_path):
    try:
        # 对于某些视频格式，可以尝试从文件属性中获取创建时间
        stat = os.stat(video_path)
        creation_time = datetime.fromtimestamp(stat.st_ctime)
        return creation_time.strftime("%Y:%m:%d %H:%M:%S")
    except Exception as e:
        print(f"获取 {video_path} 拍摄时间失败: {e}")
        return None

def move_videos(source_dir):
    for root, dirs, files in os.walk(source_dir):
        for file in files:
            file_path = os.path.join(root, file)
            if file.endswith(('.MP4','.mp4', '.mkv', '.avi', '.MOV', '.mov')):  # 可根据实际支持的视频格式添加
                try:
                    date_taken = get_video_creation_time(file_path)
                    if date_taken:
                        year = time.strftime("%Y", time.strptime(date_taken, "%Y:%m:%d %H:%M:%S"))
                        # 创建对应年份的文件夹
                        target_dir = os.path.join(source_dir, f"{year}年")
                        if not os.path.exists(target_dir):
                            os.makedirs(target_dir)
                        # 移动视频
                        os.rename(file_path, os.path.join(target_dir, file))
                except Exception as e:
                    print(f"处理 {file_path} 时出错: {e}")


# 指定目录
source_dir = "F:\\糖宝照片\\2019年"
move_videos(source_dir)